﻿Imports System.Security.Cryptography
Imports BUS
Imports DTO

Public Class frmDangKy

    Dim url As String = ""

    Private Sub frmDangKy_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        frmDangNhap.Show()
    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim open As New OpenFileDialog
        open.Filter = "JPEG(jpg)|*.jpg"
        open.InitialDirectory = "C:\"

        If open.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Dim infoReader As System.IO.FileInfo
            infoReader = My.Computer.FileSystem.GetFileInfo(open.FileName.ToString)

            If infoReader.Length > 300 * 1028 Then
                MessageBox.Show("File quá lớn")
                Return
            End If
        End If

        url = open.FileName.ToString
        PictureBox1.BackgroundImage = System.Drawing.Image.FromFile(open.FileName.ToString)

    End Sub

    Private Sub frmDangKy_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cbbGiayToTuyThan.DataSource = GiayToTuyThan_BUS.loadData
        cbbGiayToTuyThan.DisplayMember = "tenGTTT"
        cbbGiayToTuyThan.ValueMember = "maGTTT"

        cbbTenMien.Items.Add("yahoo.com")
        cbbTenMien.Items.Add("yahoo.com.vn")
        cbbTenMien.Items.Add("gmail.com")
        cbbTenMien.Items.Add("student.hcmus.edu.vn")
        cbbTenMien.Items.Add("fit.hcmus.edu.vn")

        cbbGioiTinh.Items.Add("Nam")
        cbbGioiTinh.Items.Add("Nữ")
    End Sub

    Private Sub btnDangKy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDangKy.Click
        Dim dg As New DocGia_DTO
        dg.tenDG = txtHoTen.Text
        dg.gioiTinh = cbbGioiTinh.Text
        dg.ngaySinh = dtNgaySinh.Value
        If txtTenEmail.Text = "" Then
            MessageBox.Show("Email không được để trống!")
            Return
        End If
        dg.email = txtTenEmail.Text + "@" + cbbTenMien.Text
        dg.dienThoai = txtSDT.Text
        dg.maGTTT = cbbGiayToTuyThan.SelectedValue
        If cbbGiayToTuyThan.SelectedValue = 1 And cbbGiayToTuyThan.SelectedValue = 3 Then
            dg.maLoaiDG = 2
        ElseIf cbbGiayToTuyThan.SelectedValue = 2 Then
            dg.maLoaiDG = 1
        ElseIf cbbGiayToTuyThan.SelectedValue = 4 Then
            dg.maLoaiDG = 3
        End If

        'Kiem tra doc gia viet trong bus
        If DocGia_BUS.ktGiaTriDocGia(dg) = True Then
            Dim tk As New TaiKhoan_DTO
            tk.tenTK = txtTenTaiKhoan.Text
            tk.matKhau = txtMatKhau.Text

            'kiem tra tai khoan trong bus
            If TaiKhoan_BUS.ktMatKhauTaiKhoan(tk) Then
                Dim tdg As New TheDocGia_DTO
                tdg.ngayDK = DateTime.Now
                If url = "" Then
                    MessageBox.Show("Vui lòng chọn hình đại diện!")
                    Return
                End If
                tdg.hinhAnh = url

                Dim kt As Boolean = False

                DocGia_BUS.ThemDocGia(dg, tk, tdg, kt)

                If kt Then
                    MessageBox.Show("Đăng ký thành công!")
                End If
            End If
        End If

    End Sub
End Class